package com.wgts.wgts.mvp.view.activity.login;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import com.wgts.wgts.R;
import com.wgts.wgts.base.BaseActivity;
import com.wgts.wgts.control.eventbus.LoginEvenBus;
import com.wgts.wgts.control.eventbus.PayEvenBus;
import com.wgts.wgts.control.toast.MoHuToast;
import com.wgts.wgts.dagger.ComponentHolder;
import com.wgts.wgts.dagger.component.DaggerLoginActivityComponent;
import com.wgts.wgts.dagger.module.LoginActivityModule;
import com.wgts.wgts.mvp.presenter.ILoginActivityPersenter;
import com.wgts.wgts.mvp.view.ILoginActivityView;
import com.wgts.wgts.netmodle.resp.LoginEntity;
import com.wgts.wgts.thirdParty.push.PushTools;
import com.wgts.wgts.tools.AppInfor;

import org.greenrobot.eventbus.EventBus;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/*
 *
 *    登录页面
 *   data:2018/9/1310:41
 *
 */public class LoginActivity extends BaseActivity implements ILoginActivityView {
    @Inject
    ILoginActivityPersenter persenter;

    @BindView(R.id.back_image)
    ImageView backImage;
    @BindView(R.id.login_phone_et)
    EditText loginPhoneEt;
    @BindView(R.id.phone_ly)
    RelativeLayout phoneLy;
    @BindView(R.id.login_password_et)
    EditText loginPasswordEt;
    @BindView(R.id.password_ly)
    RelativeLayout passwordLy;
    @BindView(R.id.login_ly)
    RelativeLayout loginLy;
    @BindView(R.id.register_ly)
    LinearLayout registerLy;
    @BindView(R.id.forget_password_ly)
    LinearLayout forgetPasswordLy;
    public static final String PHONE = "phone";
    public static final String PASSWORD = "password";

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.wgts_login_activity);
        ButterKnife.bind(this);
    }

    @Override
    public void init() {
        DaggerLoginActivityComponent
                .builder()
                .appComponent(ComponentHolder.getInstance().getAppComponent())
                .loginActivityModule(new LoginActivityModule(this))
                .build()
                .inject(this);
    }


    @Override
    public void wxLoginResult(LoginEntity entity) {

    }

    @Override
    public void wgtsLoginResult(LoginEntity entity) {
        dialogDismiss();
        MoHuToast.showToast("登录成功");
        //设置别名
        PushTools.getInstance().setAlias(this, entity.alia);

        AppInfor.getInstance().setVipFansCount(entity.vipFansCount);
        AppInfor.getInstance().setSvipFansCount(entity.svipFansCount);
        AppInfor.getInstance().setAccessToken(entity.accessToken);
        AppInfor.getInstance().setRefreshToken(entity.refreshToken);
        AppInfor.getInstance().setUserState(entity.userRole);
        AppInfor.getInstance().setUserPhone(entity.userName);
        AppInfor.getInstance().setUserName(entity.userNickName);
        AppInfor.getInstance().setUserHead(entity.userImage);
        AppInfor.getInstance().setInviteCode(entity.userCode);
        AppInfor.getInstance().setUserYQCode(entity.userCode);
        AppInfor.getInstance().setIllegal(entity.illegal);
        EventBus.getDefault().post(new PayEvenBus());
        EventBus.getDefault().post(new LoginEvenBus());
        finish();
        //登录成功
    }

    @Override
    public void onError(int status, String msg) {
        dialogDismiss();
        MoHuToast.showToast(msg);
    }


    @OnClick({R.id.back_image, R.id.login_ly, R.id.register_ly, R.id.forget_password_ly})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.back_image:
                finish();
                break;
            case R.id.login_ly:
                login();
                break;
            case R.id.register_ly:
                Intent intent = new Intent(LoginActivity.this, BindPhoneActivity.class);
                startActivityForResult(intent, BindPhoneActivity.BIND_PHONE_RESULT);
                break;
            case R.id.forget_password_ly:
                Intent intent1 = new Intent(LoginActivity.this, PhoneLoginActivity.class);
                startActivity(intent1);
                break;
        }
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data){
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode){
            case BindPhoneActivity.BIND_PHONE_RESULT:
                if (data != null){
                    loginPhoneEt.setText(data.getStringExtra(BindPhoneActivity.PHONE));
                    loginPasswordEt.setText(data.getStringExtra(BindPhoneActivity.PASSWORD));
                }
                break;
        }
    }

    @Override
    public void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        loginPhoneEt.setText(intent.getStringExtra(PHONE));
        loginPasswordEt.setText(intent.getStringExtra(PASSWORD));
    }

    private void login(){
        if(loginPhoneEt.getText().toString().trim().length() != 11){
            MoHuToast.showToast("请输入正确的手机号");
            return;
        }
        if(loginPasswordEt.getText().toString().trim().length() < 6 || loginPasswordEt.getText().toString().trim().length() > 16){
            MoHuToast.showToast("请输入6-16位密码");
            return;
        }
        showDialog();
        persenter.wgtsLogin(loginPhoneEt.getText().toString().trim(), loginPasswordEt.getText().toString().trim());
    }

}
